package com.jiuqi.abc.calculator.controller;

import com.jiuqi.abc.calculator.service.ABCUnitService;
import com.jiuqi.abc.intf.ABCIntfException;
import com.jiuqi.abc.intf.provider.unit.ValidateAndOutputUnitObj;
import com.jiuqi.abc.model.vo.ValidateAndOutputTreeVo;
import com.jiuqi.np.core.restapi.annotation.PermissionsResourceGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 龚泽楷
 * @date 2023年05月31日 17:35
 */
@Api(tags = {"abc单位管理"})
@RestController
@RequestMapping("abc/unit")
@PermissionsResourceGroup("abc单位管理")
public class ABCUnitController {

    private static final String STATUS = "status";
    @Autowired
    private ABCUnitService abcUnitService;

    @ApiOperation("获取下级单位")
    @GetMapping("children/{period}/{unitCode}")
    public Map<String, Object> getChildren(@PathVariable("period") String period, @PathVariable("unitCode") String unitCode) throws ABCIntfException {
        List<ValidateAndOutputTreeVo> treeNodeVoList = abcUnitService.getChildren(period, unitCode);

        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        result.put("childrenUnitList", treeNodeVoList);
        result.put(STATUS, "成功 ");

        return result;
    }

    @ApiOperation("获取全部单位")
    @GetMapping("all/{period}")
    public Map<String, Object> getWholeUnit(@PathVariable("period") String period) throws ABCIntfException {
        Map<String, List<ValidateAndOutputUnitObj>> wholeUnit = abcUnitService.getWholeUnit(period);

        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        result.put("allUnitList", wholeUnit);
        result.put(STATUS, "成功");

        return result;
    }

    @ApiOperation("根据单位编码和单位名称模糊匹配")
    @GetMapping("fuzzyMatch/{period}")
    public Map<String, Object> fuzzyMatch(@PathVariable("period") String period,@RequestParam("searchKey") String searchKey) {
        List<ValidateAndOutputUnitObj> unitObjList = Objects.equals(searchKey, "") ?
                Collections.emptyList()
                :
                abcUnitService.fuzzyMatchByUnitName(period, searchKey);
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        result.put("matchedList", unitObjList);
        result.put(STATUS, "成功");

        return result;
    }

    @ApiOperation("获取目标单位溯源路径")
    @GetMapping("/getTracePath/{period}/{unitCode}")
    public List<String> getTracePath( @PathVariable("period") String period, @PathVariable("unitCode") String unitCode) {
        return abcUnitService.getPathById(period,unitCode);
    }


}
